﻿using BCCommon;
using BCEntity.TMS.TrainingSite.TrainingSiteBusinessHours;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;

namespace BCData.TMS.TrainingSite.TrainingSiteBusinessHours
{
    public class TrainingSiteBusinessHoursData : ITrainingSiteBusinessHoursData
    {
        private readonly IDatabaseContext database;

        public TrainingSiteBusinessHoursData(IDatabaseContext database)
        {
            this.database = database;
        }

        private const string QUERY = @"SELECT
		                                TrainingSiteBusinessHoursId,
		                                TrainingSiteConfigId,
		                                AgentRecordId,
		                                BusinessDay,
		                                StartHour,
                                        StartMinute,
                                        EndHour,
		                                EndMinute,
		                                IsBusiness,
		                                CreateTime,
		                                UpdateTime 
                                    FROM
                                        TrainingSiteBusinessHours";

        public async Task<int> InsertAll(List<TrainingSiteBusinessHoursEntity> entities)
        {
            return await database.Master.InsertAllAsync(entities, database.Transaction);
        }

        public async Task<IEnumerable<TrainingSiteBusinessHoursEntity>> GetByTrainingSiteConfigId(long trainingSiteConfigId)
        {
            return await database.Slave.QueryAsync<TrainingSiteBusinessHoursEntity>($"{QUERY} WHERE TrainingSiteConfigId = ?TrainingSiteConfigId", new { trainingSiteConfigId });
        }

        public async Task<int> UpdateAll(List<TrainingSiteBusinessHoursEntity> entities)
        {
            return await database.Master.UpdateAllAsync(entities, database.Transaction);
        }

        public async Task<int> DeleteByTrainingSiteConfigId(long trainingSiteConfigId)
        {
            return await database.Master.ExecuteAsync("DELETE FROM TrainingSiteBusinessHours WHERE TrainingSiteConfigId = ?TrainingSiteConfigId", new { trainingSiteConfigId }, database.Transaction);
        }

        public async Task<IEnumerable<TrainingSiteBusinessHoursEntity>> GetListByConfigIds(List<long> trainingSiteConfigIds)
        {
            return await database.Slave.QueryAsync<TrainingSiteBusinessHoursEntity>($"{QUERY} WHERE FIND_IN_SET(TrainingSiteConfigId,?TrainingSiteConfigIds)", new { TrainingSiteConfigIds = string.Join(",", trainingSiteConfigIds) });
        }

        public async Task<IEnumerable<TrainingSiteBusinessHoursEntity>> GetList(DayOfWeek dayOfWeek, long? trainingSiteConfigId)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("IsBusiness = ?IsBusiness");
            parameters.Add("?IsBusiness", true, DbType.Boolean);
            wheres.Add("BusinessDay = ?BusinessDay");
            parameters.Add("?BusinessDay", dayOfWeek, DbType.AnsiString);
            if (trainingSiteConfigId.HasValue)
            {
                wheres.Add("TrainingSiteConfigId = ?TrainingSiteConfigId");
                parameters.Add("?TrainingSiteConfigId", trainingSiteConfigId.Value, DbType.Int64);
            }
            string sql = string.Format("{0} {1}", QUERY, wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty);
            return await database.Slave.QueryAsync<TrainingSiteBusinessHoursEntity>(sql, parameters);
        }
    }
}
